package com.zyt.controller;

import cn.hutool.db.Session;
import com.zyt.entity.Address;
import com.zyt.entity.User;
import com.zyt.service.AddressService;
import com.zyt.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AddressController {
    @Autowired
    private AddressService addressService;
    @RequestMapping("/selectAllAddress")
    private String selectAllAddress(Integer state1,HttpSession session,Model model) {
        User user=(User) session.getAttribute("user");
        Integer userId = user.getId();
        List<Address> addresses = addressService.selectAllAddress(userId);
        model.addAttribute("addresses",addresses);
        System.out.println(state1);
        if (state1==1){
            return "/address/my_address.jsp";
        }else{
           return "/order/address_form.jsp";
        }

    }
    @RequestMapping("/deleteById")
    private String deleteById(Integer id) {
        addressService.deleteById(id);
        return "/selectAllAddress.do";
    }
    @RequestMapping("/insertAll")
    private String insertAll(String name,String address,String mobile) {
       addressService.insertAll(name,address,mobile);
        return "/selectAllAddress.do";
    }
    @RequestMapping("/updateSelectedState")
    private String updateSelectedState(Integer addressId,HttpSession session) {
        User user=(User) session.getAttribute("user");
        addressService.updateSelectedState(user.getId(),addressId);
        return "/selectAllAddress.do";
    }
}
